package com.yunpai.SingleMolde;

/**
 * 懒汉式(安全)同步方法
 */
public class SinglelazyType04 {
    private static SinglelazyType04 singlelazyType04;
    private SinglelazyType04() {

    }

    /**
     * 当调 getInstance方法的时候,才会创建单例对象 ,懒加载
     * 加入同步代码，解决代码不安全
     */
    public static synchronized SinglelazyType04 getInstance() {
        if (singlelazyType04 == null) {
            singlelazyType04 = new SinglelazyType04();
        }
        return singlelazyType04;
    }
}


class Main4 {


    /**
     * 测试
     */
    public static void main(String[] args) {
        SinglelazyType04 singlelazyType = SinglelazyType04.getInstance();
        SinglelazyType04 singlelazyType1 = SinglelazyType04.getInstance();
        System.out.println(singlelazyType == singlelazyType1);
        System.out.println("singlelazyType.hashCode=" + singlelazyType.hashCode());
        System.out.println("singlelazyType1.hashCode=" + singlelazyType1.hashCode());
        //结果相同,说明是同一个对象实列
    }
}
